import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import {getRDBStore,createTable} from "../../db/dbUtils"

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    console.log("onCreate");
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy() {
    console.log("onDestroy");
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    console.log("onWindowStageCreate");
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Welcome', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });

    let context = this.context;
    let pathDir = context.filesDir;
    console.log("pathDir",pathDir);
    AppStorage.SetOrCreate("pathDir",pathDir);

     // 获取数据库对象
     getRDBStore(this.context).then((store) => {
       AppStorage.SetOrCreate("store",store);
       // 在数据库里创建表
       createTable(store).then(()=>{
         console.log("创建表成功!");
       }).catch(err=>{
         console.log("创建表失败",err.message);
       })

     }).catch(err=>{
       console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
       return;
     })

  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    console.log("onForeground")
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    console.log("onBackground")
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
